<template>
    <view class="uni-page-body" style="box-sizing: border-box;">

        <city-picker ref="classifyPicker" :dataList="classifyPickerData" :value="'id'" @onConfirm="onConfirmClass"></city-picker>
        <scroll-view class="" scroll-y="true" :style="'height:'+scrollHeight+'px;'">

            <view class="swiper-list" style="padding-top: 5px;height: auto;">
                <choose :count="count" :imgList="imgList" @changes="fileChange"></choose>
            </view>
            <compress ref="compress" :maxwh="maxwh" :quality="quality"> </compress>

            <view class="list">
                <view class="li">
                    <view class="uni-row uni-flex" style="justify-content: space-between;background: #fff;padding: 10px;">

                        <view class="uni-flex" style="align-self: center;width: 160upx;">
                            标题
                        </view>
                        <input :class="form.title?'':'red'" type="text" v-model="form.title" placeholder="请选择" style="background: #f3f3f3;padding: 5px;flex: 1;" />
                    </view>
                </view>
                <view class="li">
                    <view class="uni-row uni-flex" style="justify-content: space-between;background: #fff;padding: 10px;">

                        <view class="uni-flex" style="align-self: center;width: 160upx;">
                            分类
                        </view>
                        <input :class="form.category_id?'':'red'" type="text" disabled="true" :value="classify"
                            placeholder="请选择" style="background: #f3f3f3;padding: 5px;flex: 1;" @tap="classifyTap" />
                    </view>
                </view>
                <view class="li">
                    <view class="uni-row uni-flex" style="justify-content: space-between;background: #fff;padding: 10px;">

                        <view class="uni-flex" style="align-self: center;width: 160upx;">
                            默认价格￥
                        </view>
                        <input :class="form.price?'':'red'" type="number" v-model="form.price" placeholder="请输入价格"
                            style="background: #f3f3f3;padding: 5px;flex: 1;" />
                    </view>
                </view>
                <view class="li">
                    <view class="uni-row uni-flex" style="justify-content: space-between;background: #fff;padding: 10px;">

                        <view class="uni-flex" style="align-self: center;width: 160upx;">
                            库存数量
                        </view>
                        <input :class="form.stock?'':'red'" type="number" v-model="form.stock" placeholder="请输入库存数量"
                            style="background: #f3f3f3;padding: 5px;flex: 1;" />
                    </view>
                </view>
                <view class="li">
                    <view class="uni-row uni-flex" style="justify-content: space-between;background: #fff;padding: 10px;">

                        <view class="uni-flex" style="align-self: center;width: 160upx;">
                            地区
                        </view>
                        <input :class="form.category_id?'':'red'" type="text" disabled="true" :value="address"
                            placeholder="请选择" style="background: #f3f3f3;padding: 5px;flex: 1;" @click="showMulLinkageThreePicker" />
                    </view>
                </view>
                <city-picker ref="cityPicker" :dataList="cityPickerData" :label="'label'" @onConfirm="onConfirmCity"></city-picker>
            </view>

            <view class="list uni-row uni-flex">

                <view class="uni-flex uni-row" style="justify-content: center;align-items: center;background: #00B26A;color: #fff;padding: 0 5px;writing-mode:tb-rl">
                    属性
                </view>

                <view class="li uni-flex-item">
                    <view class="li uni-flex-item" v-for="(item,i) in attribute" :key="item.name?item.name:i+'attribute'">
                        <view class="uni-row uni-flex" style="justify-content: space-between;background: #fff;padding: 10px;">
                            <view class="uni-flex" style="align-self: center;padding-right: 5px;">
                                <input :class="item.name?'':'red'" class="uni-flex-item" type="text" v-model="item.name"
                                    placeholder="请输入属性名" style="background: #f3f3f3;padding: 5px;flex: 1;" />
                            </view>
                            <view class="uni-flex" style="align-self: center;">
                                <input :class="item.value?'':'red'" class="uni-flex-item" type="text" v-model="item.value"
                                    placeholder="请输入属性内容" style="background: #f3f3f3;padding: 5px;flex: 1;" />
                            </view>
                            <view class="uni-flex iconfont" style="align-self: center;" @tap="delTable('attribute',i)">
                                &#xe66c;
                            </view>
                        </view>
                    </view>
                    <view class="li uni-flex-item" @tap="addTable('attribute')">
                        <view class="uni-row uni-flex" style="justify-content: center;background: #fff;padding: 10px;">
                            添加
                        </view>
                    </view>
                </view>

            </view>

            <view class="list uni-row uni-flex" v-for="(item,i) in version" :key="item.name?item.name:i+'type'">

                <view class="uni-flex uni-column" style="text-align: center;justify-content: center;align-items: center;background: #00B26A;color: #fff;padding: 0 8px;">
                    <view class="">
                        版
                    </view>
                    <view class="">
                        本
                    </view>
                    <view class="">
                        {{i+1}}
                    </view>
                </view>

                <view class="li uni-flex-item" style="padding: 0 5px;">
                    <view class="li uni-flex-item" style="background: #EEEEEE;">
                        <view class="uni-row uni-flex" style="justify-content: space-between;background: #fff;padding: 10px;">
                            <view class="uni-flex uni-flex-item uni-column" style="align-self: center;">
                                <view class="">

                                    <input :class="item.name?'':'red'" class="uni-flex-item" type="text" v-model="item.name"
                                        placeholder="请输入版本名" style="background: #f3f3f3;padding: 5px;flex: 1;" />
                                </view>
                                <view class="list uni-column uni-flex">


                                    <view v-for="(compose,idx) in item.compose" :key="compose.name?compose.name:idx+'compose'">
                                        <view class="li uni-flex uni-flex-item uni-row" style="background: #EEEEEE;">


                                            <view class="uni-flex uni-column" style="justify-content: center;align-items: center;background: #00B26A;color: #fff;padding: 0 8px;">
                                                <view class="">
                                                    套
                                                </view>
                                                <view class="">
                                                    系
                                                </view>
                                                <view>
                                                    {{idx+1}}
                                                </view>
                                            </view>
                                            <view class="li uni-flex-item">
                                                <view class="uni-row uni-flex" style="justify-content: space-between;padding: 5px;box-sizing: border-box;">
                                                    <view class="uni-column uni-flex-item" style="background: #fff;padding: 5px 0;">
                                                        <view class="uni-flex" style="padding: 1px;">
                                                            <view class="uni-flex uni-row" style="justify-content:flex-end;width: 70px;align-items: center;padding-right: 5px;box-sizing: border-box;">
                                                                图片
                                                            </view>

                                                            <view :class="compose.image?'':'red'" @tap="addImage(i,idx)"
                                                                class="uni-flex-item uni-flex uni-column" style="box-sizing: border-box; background: #f3f3f3;padding: 5px;flex: 1;font-size: 50px;justify-content: center;align-items: center;">
                                                                <!-- <choose :count="1" :imgList="item.image" @changes="fileChange"></choose> -->
                                                                <image v-if="compose.image" style="width: 200px;height: 200px;"
                                                                    :src="compose.image" mode="aspectFill"></image>
                                                                <text v-else>+</text>
                                                            </view>
                                                        </view>
                                                        <view class="uni-flex" style="padding: 1px;">
                                                            <view class="uni-flex uni-row" style="justify-content:flex-end;width: 70px;align-items: center;padding-right: 5px;box-sizing: border-box;">
                                                                名称
                                                            </view>

                                                            <input :class="compose.name?'':'red'" class="uni-flex-item"
                                                                type="text" v-model="compose.name" placeholder="请输入套系名"
                                                                style="background: #f3f3f3;padding: 5px;flex: 1;" />
                                                        </view>
                                                        <view class="uni-flex" style="padding: 1px;">
                                                            <view class="uni-flex uni-row" style="justify-content:flex-end;width: 70px;align-items: center;padding-right: 5px;box-sizing: border-box;">
                                                                价格￥
                                                            </view>
                                                            <input :class="compose.price?'':'red'" class="uni-flex-item"
                                                                type="number" v-model="compose.price" placeholder="价格"
                                                                style="background: #f3f3f3;padding: 5px;flex: 1;" />
                                                        </view>
                                                        <view class="uni-flex" style="padding: 1px;">
                                                            <view class="uni-flex uni-row" style="justify-content:flex-end;width: 70px;align-items: center;padding-right: 5px;box-sizing: border-box;">
                                                                数量
                                                            </view>
                                                            <input :class="compose.stock?'':'red'" class="uni-flex-item"
                                                                type="number" v-model="compose.stock" placeholder="库存数量"
                                                                style="background: #f3f3f3;padding: 5px;flex: 1;" />
                                                        </view>
                                                    </view>

                                                    <view class="uni-flex iconfont" style="align-self: center;" @tap="delTable('compose',i,idx)">
                                                        &#xe66c;
                                                    </view>
                                                </view>
                                            </view>
                                        </view>
                                    </view>
                                    <view class="uni-row uni-flex" @tap="addTable('compose',i,item)" style="background: #EEEEEE;color: #fff;padding-top: 5px;">
                                        <view class="uni-flex uni-row" style="justify-content: center;align-items: center;padding: 0 10px;background: #00B26A;color: #fff;">
                                            +
                                        </view>
                                        <view class="li uni-flex-item">
                                            <view class="uni-row uni-flex" style="justify-content: center;background: #fff;padding: 10px;color: #333333;">
                                                添加
                                            </view>
                                        </view>
                                    </view>

                                </view>
                            </view>

                            <view class="uni-flex iconfont" style="align-self: center;" @tap="delTable('version',i)">
                                &#xe66c;
                            </view>
                        </view>
                    </view>

                </view>

            </view>
            <view class="list uni-row uni-flex" @tap="addTable('version')" style="">
                <view class="uni-flex uni-row" style="justify-content: center;align-items: center;background: #00B26A;color: #fff;padding: 0 10px;">
                    +
                </view>
                <view class="li uni-flex-item">
                    <view class="uni-row uni-flex" style="justify-content: center;background: #fff;padding: 10px;">
                        添加版本
                    </view>
                </view>
            </view>

            <text-image :formData="textImageData" @changes="changesTextImage"></text-image>
            <view class="list uni-row uni-flex" @tap="addTable('content')" style="">
                <view class="uni-flex uni-row" style="justify-content: center;align-items: center;background: #00B26A;color: #fff;padding: 0 10px;">
                    +
                </view>
                <view class="li uni-flex-item">
                    <view class="uni-row uni-flex" style="justify-content: center;background: #fff;padding: 10px;">
                        添加内容
                    </view>
                </view>
            </view>
        </scroll-view>
        <button type="default" style="width: 100%;" @tap="send">提交</button>
    </view>
</template>

<script>
    import choose from "@/components/template/image/choose.vue"
    import compress from "@/components/template/image/compress.vue"
    import textImage from "@/components/template/editor/textImage.vue"

    import Request from "@/request/index.js"
    import maskCommon from "@/components/template/mask/common.vue"
    import provinceData from '@/components/picker/city-data/province.js';
    import cityData from '@/components/picker/city-data/city.js';

    import {
        Img
    } from "@/common/yc_js/index.js";
    var YCImg = Img

    var classifyDefault = [
        [{
            id: 'bianju',
            label: '主食'
        }, {
            id: 'daoyan',
            label: '炒菜'
        }, {
            id: 'cehua',
            label: '甜点'
        }]
    ]

    export default {
        name: 'newsPublish',
        components: {
            choose,
            compress,
            maskCommon,
            textImage
        },
        data() {
            return {
                scrollHeight: 1280,
                classifyPickerData: classifyDefault,
                attribute: [],
                compose: [],
                version: [],
                banner: '',
                name: '',
                isYasuo: true,
                count: 6,
                maxwh: 720,
                quality: 1,

                msgContents: ["界面显示错乱", "启动缓慢，卡出翔了", "UI无法直视，丑哭了", "偶发性崩溃"],
                stars: [1, 2, 3, 4, 5],
                imgList: [],
                classify: '',
                form: {
                    title: '',
                    price: '',
                    stock: '',
                    category_id: 0,
                    content: [{
                        text: '',
                        type: 'text'
                    }],
                },
                cityPickerData: [],
                provinceData: provinceData,
                cityData: cityData,
                address: ''
            }
        },

        computed: {
            hasLogin() {
                return this.$store.getters.hasLogin
            },
            textImageData() {
                // console.log(this.form.content)
                return this.form.content;
            }
        },
        onShow() {

        },
        onLoad(event) {
            this.cityPickerData = [this.provinceData, this.cityData];
            // console.log(this.cityPickerData)
            var systeminfo = uni.getSystemInfoSync();
            this.systeminfo = systeminfo;
            // console.log(systeminfo.windowHeight)
            this.scrollHeight = systeminfo.windowHeight - systeminfo.statusBarHeight - 46;
            // 目前在某些平台参数会被主动 decode，暂时这样处理。
            if (event.query) {
                try {
                    this.banner = JSON.parse(decodeURIComponent(event.query));
                } catch (error) {
                    this.banner = JSON.parse(event.query);
                }
                var title = this.banner.name || '';
                uni.setNavigationBarTitle({
                    title: "发布[" + title + "]"
                });
                this.classify = title;
                this.form.category_id = this.banner.id;
            }



            Request('NewsCategory_list', {
                data: {
                    quality: 2
                },

            }).then(result => {

                uni.hideLoading();

                if (result.statusCode == 200) {
                    var newsList = result.data.data;

                    newsList.sort(function(a, b) {
                        if (a.ranking < b.ranking) {
                            return -1;
                        } else if (a.ranking > b.ranking) {
                            return 1;
                        } else {
                            return 0;
                        }
                    })


                    this.classifyPickerData = [newsList.map(e => {
                        // console.log(e)
                        // e.label=e.name;
                        return {
                            id: e.id,
                            label: e.name
                        }
                    })]

                    // Storage.Sync.set('newsList', this.newsList, 84000)
                    // console.log(this.classifyPickerData)
                    // this.getList(0);
                }
            })
            // console.log(this.banner)
        },
        methods: {
            changesTextImage(e) {
                // this.form.content=e
                var formData = e.data;
                var form = this.form;
                var idx = e.index;
                // console.log(e)
                if (e.tap == 'button') {

                    uni.showActionSheet({
                        itemList: ['删除', '上移', '下移'],
                        success: (ea) => {
                            // console.log(ea)
                            switch (ea.tapIndex) {
                                case 0:


                                    var has = 0;
                                    for (var i = 0; i < formData.length; i++) {
                                        if (formData[i].type == 'text') {
                                            has++
                                        }
                                    }
                                    if (has == 1 && e.type == 'text') {
                                        uni.showModal({
                                            title: '提示',
                                            content: '就这一个文本框了，不能再删了'
                                        })
                                        return;
                                    }
                                    form.content = formData.filter((e, i) => {
                                        if (i != idx) {
                                            return e;
                                        }

                                    })
                                    this.form = form;
                                    break;
                                case 1:

                                    if (idx == 0) {
                                        uni.showModal({
                                            title: '提示',
                                            content: '你已经在最顶上啦'
                                        })
                                        return;
                                    }


                                    var item = formData[idx - 1];
                                    formData[idx - 1] = formData[idx];
                                    formData[idx] = item;
                                    form.content = form.content.slice(idx,1);
                                    // console.log(form)

                                    // console.log(form)
                                    this.form = form;
                                    setTimeout(()=>{
                                        form.content = formData
                                        this.form = form;
                                    })
                                    
                                    break;
                                case 2:

                                    if (idx == formData.length - 1) {
                                        uni.showModal({
                                            title: '提示',
                                            content: '你已经在到底啦'
                                        })
                                        return;
                                    }
                                    var item = formData[idx + 1];
                                    formData[idx + 1] = formData[idx];
                                    formData[idx] = item;
                                    form.content = form.content.slice(idx,1);
                                    // console.log(form)
                                    this.form = form;
                                    setTimeout(()=>{
                                        form.content = formData
                                        this.form = form;
                                    })

                                    break;
                                default:
                                    break;
                            }
                        }
                    })
                }

            },
            // 三级联动选择
            showMulLinkageThreePicker() {
                this.$refs.cityPicker.showPickerView(true);
                // this.$refs.cityPicker.show()
            },
            onConfirmCity(e) {

                // cityConfirm(e) {
                // console.log(e)
                this.address = e.label;
                this.form.area_id = e.data[1].value * 100;

            },
            addImage(i, idx) {

                uni.chooseImage({
                    count: 6, //默认9
                    sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
                    sourceType: ['album'], //从相册选择
                    success: (res) => {

                        var compose = [];
                        var version = this.version;
                        // var compose = this.type[i].compose[idx];
                        version[i].compose[idx].image = res.tempFilePaths[0]
                        this.version = null;
                        this.version = version;
                        // console.log(this.version)
                        // console.log(JSON.stringify(res.tempFilePaths));
                    }
                });
            },
            addTable(e, idx) {
                switch (e) {
                    case 'attribute':
                        // console.log(e)
                        // var attribute=this.attribute[i];
                        this.attribute.push({
                            name: '',
                            value: ''
                        })
                        break;
                    case 'version':
                        // console.log(e)
                        this.version.push({
                            name: '',
                            // value: '',
                            compose: [{
                                image: '',
                                name: '',
                                price: '',
                                stock: ''
                                // value: '',
                            }]
                        })
                        break;
                    case 'compose':
                        // console.log([e, idx])
                        var compose = [];
                        var version = this.version;
                        if (version[idx].compose) {
                            compose = this.version[idx].compose;
                        } else {
                            version[idx].compose = []
                        }
                        compose.push({
                            name: '',
                            value: ''
                        })
                        version[idx].compose = compose
                        this.version = null;
                        this.version = version;
                        // console.log(this.version)
                        break;
                    case 'content':
                        uni.showActionSheet({
                            itemList: ['添加图片', '添加文本'],
                            success: (e) => {
                                var form = this.form;
                                var content = form.content;
                                var idxT=0;
                                var idxI=0;
                                form.content.filter(e=>{
                                    if(e.type=='text'){
                                        idxT++;
                                    }else{
                                        idxI++;
                                    }
                                })
                                // console.log(form.content)
                                switch (e.tapIndex) {
                                    case 0:
                                        if (idxI < 8) {
                                            content.push({
                                                type: 'image',
                                                text: ''
                                            })
                                            this.form = form;
                                            // this.tapContentImg(content.length - 1)
                                        } else {
                                            uni.showModal({
                                                title: '提示',
                                                content: '不能添加太多图片了'
                                            })
                                        }
                                        break;
                                    case 1:
                                    
                                        if (idxT < 5) {
                                            content.push({
                                                type: 'text',
                                                text: ''
                                            })
                                            this.form = form;
                                        } else {
                                            uni.showModal({
                                                title: '提示',
                                                content: '不能添加太多内容了'
                                            })
                                        }
                                        break;
                                    default:
                                        break;
                                }

                                // console.log(e)

                            }
                        })
                        break;
                    default:
                        break;
                }
            },
            // tapContentImg(idx) {
            //     uni.chooseImage({
            //         count: 6, //默认9
            //         sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
            //         sourceType: ['album'], //从相册选择
            //         success: (res) => {

            //             var form = this.form;
            //             form.content[idx].text = res.tempFilePaths[0]
            //             this.form = form;
            //             // console.log(this.version)
            //             // console.log(JSON.stringify(res.tempFilePaths));
            //         }
            //     });
            // },
            // tapContent(type, idx) {

            //     uni.showActionSheet({
            //         itemList: ['删除'],
            //         success: (e) => {
            //             switch (e.tapIndex) {
            //                 case 0:

            //                     var form = this.form;
            //                     var has=0;
            //                     for (var i = 0; i < form.content.length; i++) {
            //                        if(form.content[i].type=='text'){
            //                            has++
            //                        }
            //                     }
            //                     if(has==1){
            //                         uni.showModal({
            //                             title:'提示',
            //                             content:'不能再删了'
            //                         })
            //                         return;
            //                     }
            //                     form.content = form.content.filter((e, i) => {
            //                         if (i != idx) {
            //                             return e;
            //                         }
            //                         this.form = form;
            //                     })
            //                     break;
            //                 default:
            //                     break;
            //             }
            //         }
            //     })

            // },
            delTable(e, idx) {
                switch (e) {
                    case 'attribute':
                        // console.log(e)
                        this.attribute = this.attribute.filter((
                            item, i) => {
                            // console.log([item, i, idx])
                            if (i != idx) {
                                return e
                            }
                        })
                        break;
                    case 'version':
                        // console.log(e)
                        this.version = this.version.filter((item, i) => {
                            // console.log([item, i, idx])
                            if (i != idx) {
                                return e
                            }
                        })
                        break;
                    case 'compose':
                        // console.log(e)
                        var version = this.version
                        var compose = this.version[idx].compose;

                        version[idx].compose = compose.filter((item,
                            i) => {
                            // console.log([item, i, idx])
                            if (i != idx) {
                                return e
                            }
                        })
                        if (version[idx].compose.length < 1) {
                            // 如果没有套系信息 删除当前版本列
                            this.delTable('version', idx)
                            // var versionN=[]
                            // for (let i = 0; i < version.length; i++) {
                            //     if(idx!=i){
                            //         versionN.push(version[i])
                            //     }
                            // }
                            // this.version = versionN;
                        } else {
                            this.version = version
                        }

                        break;
                    default:
                        break;
                }
            },
            onConfirmClass(e) {


                this.classify = e.label
                this.form.category_id = e.data[0].id

                uni.setNavigationBarTitle({
                    title: "发布[" + this.classify + "]"
                });
                // console.log(this.form)  
            },
            classifyTap() {
                this.$refs.classifyPicker.showPickerView(true);
            },
            // 是否压缩图片反选
            changeIndicatorDots(e) {
                this.isYasuo = !this.isYasuo
            },
            // 返回的图片列表
            fileChange(e) {
                this.imgList = e;

            },

            previewImage() { //预览图片
                uni.previewImage({
                    urls: this.imgList
                });
            },
            send() {
                var form = this.form;
                var version = this.version; //版本
                var attribute = this.attribute; //属性
                var err = {
                    msg: '',
                    ok: true
                }
                for (var i = 0; i < attribute.length; i++) {
                    if (!attribute[i].name) {
                        err.ok = false;
                        err.msg = "属性名称输入有误\n错误位置:属性" + (i + 1) +
                            "。"
                        break;
                    }
                    if (!attribute[i].value) {
                        err.ok = false;
                        err.msg = "属性值输入有误\n错误位置:属性" + (i + 1) +
                            "。"
                        break;
                    }
                }


                if (version) {
                    for (var i = 0; i < version.length; i++) {
                        for (let ls in version[i]) {

                            if (ls == 'compose') {
                                var compose = version[i].compose; //套餐
                                for (var ci = 0; ci < compose.length; ci++) {
                                    var item = compose[ci];
                                    for (var k in item) {
                                        if (!item[k]) {
                                            err.ok = false;
                                            err.msg =
                                                "套餐信息输入有误\n错误位置:版本" +
                                                (i + 1) +
                                                "套系" + (ci + 1) +
                                                "。"
                                            break;
                                        }
                                    }
                                    if (!err.ok) {
                                        break;
                                    }
                                    // console.log(item)
                                }
                            } else if (!version[i][ls]) {
                                // console.log([ls, version[i]])
                                err.ok = false;
                                err.msg = "版本信息输入有误\n错误位置:版本" + (i +
                                    1) + "。"
                                break;
                            }
                        }
                        if (!err.ok) {
                            break;
                        }


                    }
                }




                form.attribute = attribute;
                form.version = version;
                console.log(version)
                console.log(form)

                if (form.title.length < 2) {
                    err.ok = false;
                    err.msg = "标题不足2个字";

                } else if (!form.category_id) {
                    err.ok = false;
                    err.msg = "请选择分类";

                } else if (!(parseFloat(form.price) > 0)) {
                    err.ok = false;
                    err.msg = "价格输入有误";
                } else if (!parseInt(form.stock) > 0) {
                    err.ok = false;
                    err.msg = "库存输入有误";
                } else if (this.imgList.length < 1) {
                    err.ok = false;
                    err.msg = "至少添加一张图片";

                } else if (form.content.length < 1) {
                    err.ok = false;
                    err.msg = "内容不能为空";
                } else if (form.content.length > 0) {
                    var content = form.content
                    var hasText = false;
                    for (var i = 0; i < content.length; i++) {
                        switch (content[i].type) {
                            case 'image':
                                if (content[i].text.length < 1) {
                                    err.ok = false;
                                    err.msg = '图片不能为空';
                                }
                                break;
                            case 'text':
                                if (content[i].text.length < 1) {
                                    err.ok = false;
                                    err.msg = '内容不能为空';
                                }
                                hasText = true;
                                break;
                            default:

                                break;
                        }
                    }
                    if (!hasText) {
                        err.ok = false;
                        err.msg = '内容不能为空';
                    }
                }
                if (!err.ok) {
                    uni.showModal({
                        title: '提示',
                        content: err.msg
                    })
                    return;
                }

                function requst(imgs, data) {
                    var data = Object.assign({}, data);
                    // data.category_id = 17;
                    var content = data.content.split('\n');

                    data.title = content[0].substring(0, 28);
                    if (content[1]) {
                        data.content = '';
                        for (let i = 0; i < content.length; i++) {
                            if (i != 0) {
                                data.content = data.content +
                                    content[i]
                            } else {
                                if (content.length == 1) {
                                    data.content = content[i]
                                }

                            }
                        }
                    }

                    data.image = imgs; //base64 图片字符串
                    // console.log(JSON.stringify([imgs,data]));
                    Request('UserNews_add', {
                        data: {
                            form: data
                        }
                    }).then(res => {
                        if (res.statusCode === 200) {
                            uni.showToast({
                                title: "发布成功!"
                            });
                            uni.navigateBack();
                        } else {
                            uni.showToast({
                                title: "发布失败!" +
                                    res.data.message,
                                icon: 'none'
                            });
                        }
                    }).catch(e => {
                        console.log(e)
                    })

                }

                if (this.isYasuo) {
                    this.$refs.compress.yasuoImg(this.imgList).then(
                        imgs => {

                            YCImg.imgsToBase64(imgs).then(
                                base64All => {

                                    requst(base64All, this.form)
                                })

                        })
                } else {
                    YCImg.imgsToBase64(this.imgList).then(base64All => {
                        // console.log(['转成base64',base64All])
                        requst(base64All, this.form)
                    })

                }
            }
        }
    }
</script>

<style lang="scss">
    .red {
        border: 1px solid #DD524D;
    }

    .list {
        padding: 5px 0;
        box-sizing: border-box;

        .li {
            padding: 1px 0;
            box-sizing: border-box;
        }
    }
</style>
